from django.db import models
from django.contrib.auth.models import User

class MedicationSchedule(models.Model):
    FREQUENCY_TYPE_CHOICES = [
        ('daily', 'Daily'),
        ('weekly', 'Weekly'),
        ('monthly', 'Monthly'),
        ('custom', 'Custom'),
    ]
    
    family_member = models.ForeignKey('families.FamilyMember', on_delete=models.CASCADE)
    my_medicine = models.ForeignKey('medicines.MyMedicine', on_delete=models.CASCADE)
    schedule_name = models.CharField(max_length=100)
    dosage = models.CharField(max_length=100)
    frequency_type = models.CharField(max_length=10, choices=FREQUENCY_TYPE_CHOICES, default='daily')
    frequency_value = models.JSONField(null=True, blank=True)
    start_date = models.DateField()
    end_date = models.DateField(null=True, blank=True)
    enabled = models.BooleanField(default=True)
    notes = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    # 运营监控字段 - 对应用药计划页时间轴视图与任务完成状态
    completion_rate = models.FloatField(default=0.0, help_text="计划完成率(%)")
    last_completed_at = models.DateTimeField(null=True, blank=True, help_text="最后完成时间")
    missed_count = models.IntegerField(default=0, help_text="错过次数统计")
    delayed_count = models.IntegerField(default=0, help_text="延迟次数统计")

    def __str__(self):
        return f"{self.schedule_name} - {self.family_member.user.username}"


class MedicationRecord(models.Model):
    STATUS_CHOICES = [
        ('taken', 'Taken'),
        ('missed', 'Missed'),
        ('skipped', 'Skipped'),
        ('delayed', 'Delayed'),
    ]
    
    schedule = models.ForeignKey(MedicationSchedule, on_delete=models.CASCADE)
    family_member = models.ForeignKey('families.FamilyMember', on_delete=models.CASCADE)
    medicine = models.ForeignKey('medicines.Medicine', on_delete=models.CASCADE)
    scheduled_time = models.DateTimeField()
    taken_time = models.DateTimeField(null=True, blank=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='missed')
    dosage_taken = models.CharField(max_length=100, blank=True)
    notes = models.TextField(blank=True)
    side_effects_noted = models.TextField(blank=True)
    created_by = models.ForeignKey(User, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    
    # 运营监控字段 - 对应用药计划页任务完成状态
    response_time = models.IntegerField(null=True, blank=True, help_text="响应时间(分钟)")
    early_completion = models.BooleanField(default=False, help_text="是否提前完成")

    def __str__(self):
        return f"{self.medicine.name} - {self.status}"


class Reminder(models.Model):
    CHANNEL_CHOICES = [
        ('push', 'Push'),
        ('sms', 'SMS'),
        ('email', 'Email'),
        ('in_app', 'In App'),
    ]
    
    STATUS_CHOICES = [
        ('pending', 'Pending'),
        ('sent', 'Sent'),
        ('failed', 'Failed'),
        ('read', 'Read'),
    ]
    
    schedule = models.ForeignKey(MedicationSchedule, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    reminder_time = models.DateTimeField()
    sent_time = models.DateTimeField(null=True, blank=True)
    channel = models.CharField(max_length=10, choices=CHANNEL_CHOICES, default='push')
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='pending')
    response_data = models.JSONField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    
    # 运营监控字段 - 对应用药计划页提醒推送状态
    delivery_time = models.FloatField(null=True, blank=True, help_text="送达时间(秒)")
    read_time = models.DateTimeField(null=True, blank=True, help_text="阅读时间")
    retry_count = models.IntegerField(default=0, help_text="重试次数")

    def __str__(self):
        return f"Reminder for {self.user.username} at {self.reminder_time}"